package com.example.order;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.order.fragment.AdminManageUserFragment;
import com.example.order.fragment.AdminManageWindowFragment;
import com.example.order.fragment.ConsumerOrderDetailFragment;
import com.example.order.fragment.ConsumerOrderFoodFragment;
import com.google.android.material.tabs.TabLayout;

public class ConsumerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);

        ViewPager viewPager = findViewById(R.id.viewPager);
        TabLayout tabLayout = findViewById(R.id.tabLayout);

        // 获取传递的 consumer_id
        String consumer_id = getIntent().getStringExtra("consumer_id");

        // 创建适配器
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), consumer_id);
        viewPager.setAdapter(pagerAdapter);

        // 将 TabLayout 与 ViewPager 相关联
        tabLayout.setupWithViewPager(viewPager);
    }

    // 创建适配器类
    private static class PagerAdapter extends FragmentPagerAdapter {

        private static final int NUM_PAGES = 2;
        private String consumer_id;

        public PagerAdapter(FragmentManager fm, String consumer_id) {
            super(fm);
            this.consumer_id = consumer_id;
        }

        @Override
        public Fragment getItem(int position) {
            // 根据位置返回对应的 Fragment
            switch (position) {
                case 0:
                    // 在创建 ConsumerOrderFoodFragment 实例之前将 consumer_id 添加到 Bundle 中
                    ConsumerOrderFoodFragment fragment = new ConsumerOrderFoodFragment();
                    Bundle args = new Bundle();
                    args.putString("consumer_id", consumer_id);
                    fragment.setArguments(args);
                    return fragment;
                case 1:
                    // 在创建 ConsumerOrderDetailFragment 实例之前将 consumer_id 添加到 Bundle 中
                    ConsumerOrderDetailFragment detailFragment = new ConsumerOrderDetailFragment();
                    Bundle detailArgs = new Bundle();
                    detailArgs.putString("consumer_id", consumer_id);
                    detailFragment.setArguments(detailArgs);
                    return detailFragment;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // 设置每个标签的标题
            switch (position) {
                case 0:
                    return "订餐";
                case 1:
                    return "订单";
                default:
                    return null;
            }
        }
    }
}
